var gpsoo = new GpsooSDK();		//gpsoo服务器封装初始化
var msgId = 0;
var messageType = [];
messageType[1] = "断电警告";
messageType[2] = "位移警告";
messageType[3] = "超速警告";
messageType[4] = "振动警告";
var cars;						//车对象数组
var deviceInfoStr = ["在线", "未上线", "过期", "离线"];
var map;
var firstLoad = true;			//第一次启用的标志
var initPage = function (size) {
    map = new BMap.Map("mapDiv"); // 创建地图实例
    var point = new BMap.Point(116.404, 39.915); // 创建点坐标
    map.centerAndZoom(point, size); // 初始化地图，设置中心点坐标和地图级别
    var opts = {offset: new BMap.Size(150, 5)};
    map.addControl(new BMap.NavigationControl());
    map.addControl(new BMap.OverviewMapControl());
    map.addControl(new BMap.MapTypeControl());
    map.addControl(new BMap.ScaleControl(opts));
    map.enableScrollWheelZoom();	//启用滚轮放大缩小，默认禁用
    map.enableContinuousZoom();		//启用地图惯性拖拽，默认禁用
};



var focusOnCar = function (car) {
    var lng = car.gpsooCar.lng;
    var lat = car.gpsooCar.lat;
    var point = new BMap.Point(lng, lat);
    map.panTo(point); // 初始化地图，设置中心点坐标和地图级别
};


/**
 * 把数据cars里面的东西都画出来*/
var paintCarInMap = function (imeis, callback, param) {
    gpsoo.loadTracking(imeis, function (data) {
        var gpsooCars = data.rows;
        for (i = 0, j = 0; i < gpsooCars.length; i++, j++) {//j是本地服务器的下标
            if (cars[i].marker != undefined) {
                map.removeOverlay(cars[i].marker);
            }
            var j = i;
            while (cars[j].carImei != gpsooCars[i].imei) {
                j++;
            }
            cars[i].gpsooCar = gpsooCars[i];

            var pt = [];
            var myIcon = [];
            pt[i] = new BMap.Point(cars[i].gpsooCar.lng, cars[i].gpsooCar.lat);

            if (firstLoad) {
                //第一次加载自动跳转
                focusOnCar(cars[i]);
                firstLoad = false;
            }

            myIcon[i] = new BMap.Icon(gpsoo.getCarImage(cars[i].gpsooCar.course), new BMap.Size(25, 25));
            cars[i].marker = new BMap.Marker(pt[i], {icon: myIcon[i]});  // 创建标注
            map.addOverlay(cars[i].marker);
            if (callback != undefined && callback != null) {
                callback(cars[i], param);
            }
        }

    })
};
var showAlertDetail = function (alert) {
    clearAndStopPaintCar();
    var alertPoint = new BMap.Point(alert.alertPoslong, alert.alertPoslat);
    var marker = new BMap.Marker(alertPoint);
    map.centerAndZoom(alertPoint, 18);
    map.addOverlay(marker);
    var opts = {
        width: 200,     // 信息窗口宽度
        title: alert.carImei, // 信息窗口标题
        enableMessage: false//设置允许信息窗发送短息
    };
    var infoWindow = new BMap.InfoWindow(alertType[alert.alertType] +
    "<br />地点：" + alert.alertPosname +
    "<br />时间：" + longToString(alert.alertTime), opts);  // 创建信息窗口对象
    marker.addEventListener("click", function () {
        map.openInfoWindow(infoWindow, alertPoint); //开启信息窗口
    });
};

var showedMsgArray = [];

var updateMsg = function (messageId) {
    $.get(CONTEXT + '/ajax/message/read?messageId=' + messageId, function (data) {
        console.log("mark as read");
        console.log(data);
    });
};

/**显示警告*/
var showMsg = function (message) {
    if (showedMsgArray[message.messageId] != 1) {
        showedMsgArray[message.messageId] = 1;
        var nothingFoundHtml = '<div class="alert alert-warning alert-dismissible" role="alert" id="alert_' + msgId + '">' +
            '<button type="button" class="close closeMsg" id="closeMsg_' + message.messageId + '" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
            '<strong>' + messageType[message.alertType] + '</strong> 车牌号：' + message.carNum + ' 地点：' + message.alertPosname + ' ——' + longToString(message.alertTime);
            '</div>';
        msgId++;
        $("#alertDiv").append(nothingFoundHtml);
        $("#closeMsg_" + message.messageId).on('click', function () {
            updateMsg(message.messageId);
        });
    }

};
var readMessage = function () {
    $.get(CONTEXT + '/ajax/message/getUnread', function (data) {
        if (data.rows.length != 0) {
            for (var i = 0; i < data.rows.length; i++) {
                showMsg(data.rows[i]);
            }
        }
    });
};


$(document).ready(function () {
    setInterval("readMessage()", 1000);
});

